package com.auxiliary;

import java.util.ArrayList;
import java.util.ArrayList;

import com.myidis.response.BBRes;

public class BBForPeakValley {

	private int halfPeriod;
	private int onePeriod;
	private String springDay;
	
	private String start;
	
	private ArrayList<Double> analysis;
	
	private SeasonAdjuster adjuster;
	
	public BBForPeakValley() {
		this.halfPeriod = 6;
		this.onePeriod = 15;
		this.start = null;
		this.analysis = new ArrayList<Double>();
	}
	
	public BBForPeakValley(TimeSequence analysisSequence) {
		this.halfPeriod = 6;
		this.onePeriod = 15;
		this.start = analysisSequence.getStartDate();
		this.analysis = analysisSequence.getValueList();
		
		this.adjuster = new SeasonAdjuster(analysisSequence, analysisSequence.getFrequency(), 1);
	}


	public BBForPeakValley(ArrayList<Double> analysisSeries, String start, String springDay, int frequency)
	{
		this.analysis = analysisSeries;
		this.start=start;
		this.springDay = springDay;
		this.halfPeriod = 6;
		this.onePeriod = 15;
		this.adjuster = new SeasonAdjuster(analysisSeries,start, frequency);
	}

	public BBForPeakValley(TimeSequence analysisSequence, int halfPeriod, int onePeriod) {
		this.halfPeriod = halfPeriod;
		this.onePeriod = onePeriod;
		this.start = analysisSequence.getStartDate();
		this.analysis = analysisSequence.getValueList();

		this.adjuster = new SeasonAdjuster(analysisSequence, analysisSequence.getFrequency(), 1);
	}
	
	public BBRes calc() {
		int result[][] = calcExtremumByBB();
		BBRes bbRes = new BBRes();
		
		BasicCalculator bc = new BasicCalculator();
		
		for(int i = 0; i < result.length; i++) {
			if(result[i][1] == 0)
				bbRes.addValley(bc.addMonth(start, result[i][0]));
			else
				bbRes.addPeak(bc.addMonth(start, result[i][0]));
		}
		
		return bbRes;
	}
	
	//执行bb算法
	public int[][] calcExtremumByBB() {
		int MCD = adjuster.getMCD();
		
		ArrayList<Double> ArrayList = new ArrayList<Double>();
	
		BasicCalculator bc = new BasicCalculator();

		//修正特异值
		ArrayList = bc.removeOutlier(analysis);
		ArrayList<Double> temp = bc.movingAvg(12, ArrayList);
		int PVSeries[][] = bc.findConstriantPV(bc.findPV(temp), halfPeriod, onePeriod);
		
		//在 spencer 曲线上确定转折点
		temp = bc.DoSpencer(ArrayList);
		PVSeries = bc.adjustPV(PVSeries, MCD, temp);
		
		//进行 MCD 项移动平均，在MCD曲线上确定转折点
		temp = bc.movingAvg(MCD, analysis);
		PVSeries = bc.adjustPV(PVSeries, MCD, temp);
		
		//确定原序列的转折点
		PVSeries = bc.findConstriantPV(bc.adjustPV(PVSeries, MCD, analysis), halfPeriod, onePeriod);
		return PVSeries;

	}


}
